package com.yantong.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * 新代付单笔查询
 * @author Lori
 * @version  2018年6月11日
 */
public class SinglePayQuery {
	public static void main(String args[]) throws Exception {

		String key = "";

		Map<String, Object> map = new HashMap<>();
		map.put("userNo", "");// 商户编号 
		map.put("merchantOrderNo", "");//商户订单号 

		String reqSign = getRequestSign(map);
		// 签名
		String hmac = Md5_Sign.SignByMD5(reqSign, key);
		map.put("hmac", hmac);/** 签名数据 */

		// Map转json字符串
		String reqBodyJson = JSON.toJSONString(map);
		String httpResponseJson = HttpClientUtil
				.sendHttpPost("https://www.joinpay.com/payment/pay/singlePayQuery",reqBodyJson);

		doResponseInfo(httpResponseJson, key);
	}
	
	//单笔支付查询
	public static Map<String,Object> singlePayQueryInfo(String merchantOrderNo) throws Exception {

		String key =  ConfigUtil.getProperty("HJ_key");//商户秘钥

		Map<String, Object> map = new HashMap<>();
		map.put("userNo", ConfigUtil.getProperty("HJ_userNo"));// 商户编号 
		map.put("merchantOrderNo", merchantOrderNo);//商户订单号 

		String reqSign = getRequestSign(map);
		// 签名
		String hmac = Md5_Sign.SignByMD5(reqSign, key);
		map.put("hmac", hmac);/** 签名数据 */

		// Map转json字符串
		String reqBodyJson = JSON.toJSONString(map);
		String httpResponseJson = HttpClientUtil
				.sendHttpPost("https://www.joinpay.com/payment/pay/singlePayQuery",reqBodyJson);

		return doResponseInfoBykey(httpResponseJson, key);
	}
	
	
	/**
	 * 对单笔代付响应信息的处理
	 * 
	 * @param httpResponseJson 响应信息json字符串
	 * @param key 商户秘钥
	 * @throws Exception 异常实体类
	 */
	@SuppressWarnings("unchecked")
	private static  Map<String,Object> doResponseInfoBykey(String httpResponseJson, String key) throws Exception {
		// 响应信息map集合
		Map<String, Object> httpResponseMap = (Map<String, Object>) JSONObject.parse(httpResponseJson);
		// 业务数据map集合
		Map<String, Object> dataMap = (Map<String, Object>) httpResponseMap.get("data");
		dataMap.put("statusCode", httpResponseMap.get("statusCode"));
		dataMap.put("message", httpResponseMap.get("message"));
		
		// 请求签名串
		String reqSign = getRequestSign(httpResponseMap);
		// 响应签名串
		String respSign = getResponseSign(dataMap);
		System.out.println("字符串："+respSign);
		// 请求数据的加密签名
		String reqHmac = Md5_Sign.SignByMD5(respSign, key);
		// 请求数据的加密签名
		String respHmac = (String) dataMap.get("hmac");
		System.out.println("reqHmac：" + reqHmac);
		System.out.println("respSign：" + respHmac);
        
		reqHmac=reqHmac.toUpperCase();
		respHmac=respHmac.toUpperCase();
		boolean isMatch = respSign.equals(respSign);
		if (isMatch) {
			return dataMap;
		} else {
			return null;
		}

	}
	
	/**
	 * 对单笔代付响应信息的处理
	 * 
	 * @param httpResponseJson 响应信息json字符串
	 * @param key 商户秘钥
	 * @throws Exception 异常实体类
	 */
	@SuppressWarnings("unchecked")
	private static void doResponseInfo(String httpResponseJson, String key) throws Exception {
   
		// 响应信息map集合
		Map<String, Object> httpResponseMap = (Map<String, Object>) JSONObject.parse(httpResponseJson);
		// 业务数据map集合
		Map<String, Object> dataMap = (Map<String, Object>) httpResponseMap.get("data");
		dataMap.put("statusCode", httpResponseMap.get("statusCode"));
		dataMap.put("message", httpResponseMap.get("message"));
		
		// 请求签名串
		String reqSign = getRequestSign(httpResponseMap);
		// 响应签名串
		String respSign = getResponseSign(dataMap);
		System.out.println("字符串："+respSign);
		// 请求数据的加密签名
		String reqHmac = Md5_Sign.SignByMD5(respSign, key);
		// 请求数据的加密签名
		String respHmac = (String) dataMap.get("hmac");
		System.out.println("reqHmac：" + reqHmac);
		System.out.println("respSign：" + respHmac);
        
		reqHmac=reqHmac.toUpperCase();
		respHmac=respHmac.toUpperCase();
		boolean isMatch = respSign.equals(respSign);
		if (isMatch) {
			System.out.println("验签成功");
		} else {
			System.out.println("验签失败");
		}

	}
	
    /**
     * 获取请求数据签名串信息
     * 必须按新代付接口文档请求参数信息顺序来进行字符串的拼接，详情请参考新代付接口文档请求报文
     * 
     * @param params 请求数据参数
     * @return 返回请求签名串
     */
	public static String getRequestSign(Map<String, Object> params) {
		
		StringBuilder stringBuilder = new StringBuilder();
		stringBuilder.append(params.get("userNo"))
				.append(params.get("merchantOrderNo"));

		return stringBuilder.toString();
	}
	
	/**
     * 获取响应数据签名串信息
     * 必须按新代付接口文档应答参数信息顺序来进行字符串的拼接，详情请参考新代付接口文档的应答报文
     * 
     * @param params 响应数据参数
     * @return 返回响应签名串
     */
	public static String getResponseSign(Map<String, Object> params) {
		
		StringBuilder stringBuilder = new StringBuilder();
		stringBuilder.append(params.get("statusCode")).append(params.get("message"))
		.append(params.get("status"))
        .append(params.get("errorCode"))
        .append(params.get("errorDesc")).append(params.get("userNo"))
        .append(params.get("merchantOrderNo"));
		if(params.get("platformSerialNo") !=null) {
			stringBuilder.append(params.get("platformSerialNo"));
		};
		if(params.get("receiverAccountNoEnc") !=null) {
			stringBuilder.append(params.get("receiverAccountNoEnc"));
		};
		if(params.get("receiverNameEnc") !=null) {
			stringBuilder.append(params.get("receiverNameEnc"));
		};
		if(params.get("paidAmount") !=null) {
			stringBuilder.append(params.get("paidAmount"));
		};
		if(params.get("fee") !=null) {
			stringBuilder.append(params.get("fee"));
		};
		return stringBuilder.toString();
	}
}
